package com.zzh.ideas.web;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@ServletComponentScan
@SpringBootApplication
@EnableScheduling
@MapperScan("com.zzh.ideas.core.dao")
public class SpringBootTestApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        System.out.println(System.getProperty("test"));
        System.out.println(System.getProperty("test1"));
        SpringApplication.run(SpringBootTestApplication.class, args);
//        LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
//        //设置全局日志级别
//        ch.qos.logback.classic.Logger logger=loggerContext.getLogger("root");
//        logger.setLevel(Level.toLevel("ERROR"));

//        DebugSignalHandler.listenTo("HUP");
//        DebugSignalHandler.listenTo("INT");
//        DebugSignalHandler.listenTo("KILL");
//        DebugSignalHandler.listenTo("TERM");

    }

    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // 初始化参数
        // servletRegistrationBean.addInitParameter("allow", "10.246.172.32");
        // // 白名单
        // servletRegistrationBean.addInitParameter("deny", "192.168.1.73"); //
        // 黑名单（优先级高于白名单）
        servletRegistrationBean.addInitParameter("loginUsername", "admin"); // 查看账号
        servletRegistrationBean.addInitParameter("loginPassword", "123456"); // 查看密码
        servletRegistrationBean.addInitParameter("resetEnable", "false"); // 是否能够重置数据
        return servletRegistrationBean;
    }

    /**
     * 注册一个：filterRegistrationBean
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
